<style lang="less">
    @import "loan-activate.less";
    @import "row-background.less";
</style>
<template>
    <rui-page :defines="defines">
        <div class="search">
            <Row>
                <Form ref="searchForm" :model="searchForm" inline :label-width="100" class="search-form">
                    <Form-item label="申请编号" prop="applyNo">
                        <Input type="text" v-model="searchForm.applyNo" clearable placeholder="请输入申请编号" style="width: 200px" />
                    </Form-item>
                    <Form-item label="客户姓名" prop="custName">
                        <Input type="text" v-model="searchForm.custName" clearable placeholder="请输入客户姓名" style="width: 200px" />
                    </Form-item>
                    <span v-if="drop">
                            <Form-item label="经销商名称" prop="dealerName">
                                <Input v-model="searchForm.dealerName" type="text" clearable placeholder="请输入合作商名称" style="width:200px" />
                            </Form-item>
                            <Form-item label="激活状态" prop="actStatus">
                                <Select v-model="searchForm.actStatus" clearable placeholder="请选择激活状态" style="width:200px">
                                    <Option v-for="item in dataDic.activateStatus" :label="item.title" :value="item.value" :key="item.value">{{item.title}}</Option>
                                </Select>
                            </Form-item>
                            <Form-item label="放款审核状态" prop="loanApproveStatus">
                                <Select v-model="searchForm.loanApproveStatus" clearable placeholder="请选择放款审核状态" style="width:200px">
                                    <Option v-for="item in dataDic.conApplyStatus" :label="item.title" :value="item.value" :key="item.value">{{item.title}}</Option>
                                </Select>
                            </Form-item>
                            <Form-item label="GPS审核状态" prop="gpsApproveStatus">
                                <Select v-model="searchForm.gpsApproveStatus" clearable placeholder="请选择渠道类型" style="width:200px">
                                    <Option v-for="item in dataDic.isType" :label="item.title" :value="item.value" :key="item.value">{{item.title}}</Option>
                                </Select>
                            </Form-item>
                            <Form-item label="额度判断状态" prop="limitApproveStatus">
                                <Select v-model="searchForm.limitApproveStatus" clearable placeholder="请选择业务类型" style="width:200px">
                                    <Option v-for="item in dataDic.isType" :label="item.title" :value="item.value" :key="item.value">{{item.title}}</Option>
                                </Select>
                            </Form-item>
                            <Form-item label="放款审核通过时间" prop="loanApproveTime">
                                <DatePicker v-model="loanApproveTime" type="daterange" format="yyyy-MM-dd" clearable @on-change="selectDateRange" placeholder="选择起始时间" style="width: 200px"></DatePicker>
                            </Form-item>
                            <Form-item label="业务类型" prop="businessType">
                                <Select v-model="searchForm.businessType" clearable placeholder="请选择案件状态" style="width:200px">
                                    <Option v-for="item in dataDic.confBusinessType" :label="item.title" :value="item.value" :key="item.value">{{item.title}}</Option>
                                </Select>
                            </Form-item>
                            <Form-item label="车辆类型" prop="carType">
                                <Select v-model="searchForm.carType" clearable placeholder="请选择车辆类型" style="width:200px">
                                    <Option v-for="item in dataDic.carType" :label="item.title" :value="item.value" :key="item.value">{{item.title}}</Option>
                                </Select>
                            </Form-item>
                            <Form-item label="车辆属性" prop="carNature">
                                <Select v-model="searchForm.carNature" clearable placeholder="请选择车辆属性" style="width:200px">
                                    <Option v-for="item in dataDic.carNature" :label="item.title" :value="item.value" :key="item.value">{{item.title}}</Option>
                                </Select>
                            </Form-item>
                            <Form-item label="运营方式" prop="operateWay">
                                <Select v-model="searchForm.operateWay" clearable placeholder="请选择运营方式" style="width:200px">
                                    <Option v-for="item in dataDic.operateWay" :label="item.title" :value="item.value" :key="item.value">{{item.title}}</Option>
                                </Select>
                            </Form-item>
                            <Form-item label="挂靠方式" prop="affiliatedWay">
                                <Select v-model="searchForm.affiliatedWay" clearable placeholder="请选择挂靠方式" style="width:200px">
                                    <Option v-for="item in dataDic.affiliatedWay" :label="item.title" :value="item.value" :key="item.value">{{item.title}}</Option>
                                </Select>
                            </Form-item>
                            <Form-item label="放款模式" prop="lendingMode">
                                <Select v-model="searchForm.lendingMode" clearable placeholder="请选择放款模式" style="width:200px">
                                    <Option v-for="item in dataDic.lendingMode" :label="item.title" :value="item.value" :key="item.value">{{item.title}}</Option>
                                </Select>
                            </Form-item>
                        </span>
                    <Form-item style="margin-left: -35px;" v-if="dropDownContent=='展开'">
                        <Button @click="handleSearch" type="primary" icon="ios-search">查询</Button>
                        <Button @click="handleReset" type="primary" icon="ios-trash">重置</Button>
                        <a class="drop-down" @click="dropDown">{{dropDownContent}}
                            <Icon :type="dropDownIcon"></Icon>
                        </a>
                    </Form-item>
                    <Form-item style="margin-left: -65px;width:100%;text-align: center;" v-if="dropDownContent=='收起'">
                        <Button @click="handleSearch" type="primary" icon="ios-search">查询</Button>
                        <Button @click="handleReset" type="primary" icon="ios-trash">重置</Button>
                        <a class="drop-down" @click="dropDown">{{dropDownContent}}
                            <Icon :type="dropDownIcon"></Icon>
                        </a>
                    </Form-item>
                </Form>
            </Row>
            <Row class="operation">
                <Button type="primary" @click="backLoan">退回放款</Button>
                <Button type="primary" @click="discard">&nbsp;废弃&nbsp;</Button>&nbsp;&nbsp;&nbsp;&nbsp;
                <Button icon="md-refresh" type="primary" @click="doActivate">人工激活</Button>
                自动激活开关
                <i-switch size="large" v-model="isActivate" :before-change="handleBeforeChange">
                    <span slot="open">打开</span>
                    <span slot="close">关闭</span>
                </i-switch>
            </Row><br>
            <Row>
                <Alert show-icon>
                    已选择 <span class="select-count">{{selectCount}}</span> 项
                    <a class="select-clear" @click="clearSelectAll">清空</a>
                </Alert>
            </Row>
            <Row>
                <Table v-bind:loading="loading"
                       border
                       v-bind:columns="columns"
                       v-bind:data="data"
                       sortable="custom"
                       @on-sort-change="changeSort"
                       @on-selection-change="showSelect"
                       :row-class-name="rowClassName"
                       ref="table">
                    <template slot-scope="{row , index}" slot="loanApproveStatus">
                        <span>{{setApplyStatusTitle(row.loanApproveStatus)}}</span>
                    </template>
                    <template slot-scope="{row , index}" slot="gpsApproveStatus">
                        <span>{{setGpsApproveStatus(row.gpsApproveStatus)}}</span>
                    </template>
                    <template slot-scope="{row , index}" slot="limitApproveStatus">
                        <span>{{setGpsApproveStatus(row.limitApproveStatus)}}</span>
                    </template>
                    <template slot-scope="{row , index}" slot="actStatus">
                        <span>{{setActStatus(row.actStatus)}}</span>
                    </template>
                    <template slot-scope="{row , index}" slot="businessType">
                        <span>{{setBusinessType(row.businessType)}}</span>
                    </template>
                    <template slot-scope="{row , index}" slot="carType">
                        <span>{{setCarType(row.carType)}}</span>
                    </template>
                    <template slot-scope="{row , index}" slot="carNature">
                        <span>{{setCarNature(row.carNature)}}</span>
                    </template>
                    <template slot-scope="{row , index}" slot="operateWay">
                        <span>{{setOperateWay(row.operateWay)}}</span>
                    </template>
                    <template slot-scope="{row , index}" slot="affiliatedWay">
                        <span>{{setAffiliatedWay(row.affiliatedWay)}}</span>
                    </template>
                    <template slot-scope="{row , index}" slot="lendingMode">
                        <span>{{setLendingMode(row.lendingMode)}}</span>
                    </template>
                </Table>
            </Row>
            <Row type="flex" justify="end" class="page">
                <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize"
                      @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]"
                      size="small" show-total show-elevator show-sizer></Page>                    </Row>
            <Modal v-model="backLoanModalVisible" :title="modalTitle" :closable="false" :mask-closable='false' :width="1000" :styles="{top: '180px'}">
                            <div slot="footer">
                                <Button type="text" @click="backLoanModalVisible = false">取消</Button>
                                <Button type="primary" @click="submitBackLoan">提交</Button>
                            </div>
                            <Row>
                                <Alert show-icon>退回放款</Alert>
                                <rui-form :form-options="backLoanFormOptions" :read-only="false" ref="loanForm1" style="margin-left:13%;margin-top:-10px;height: 200px;" />
                            </Row>
                        </Modal>
            <Modal v-model="discardModalVisible" :title="modalTitle" :closable="false" :mask-closable='false' :width="1000" :styles="{top: '180px'}">
                            <div slot="footer">
                                <Button type="text" @click="discardModalVisible = false">取消</Button>
                                <Button type="primary" @click="submitDiscard">提交</Button>
                            </div>
                            <Row>
                                <Alert show-icon> 废弃案件 </Alert>
                                <rui-form :form-options="discardFormOptions" :read-only="false" ref="loanForm2" style="margin-left:13%;margin-top:-10px;height: 200px;" />
                            </Row>
                        </Modal>
        </div>
    </rui-page>
</template>
<script>
    import loanActivateDefine from '../defines/loan-activate-define'
import { getByTypes } from '_p/basic/api/admin/datadic'
import {
  getLoanActivateList,
  submitBackLoanData,
  submitDiscardData,
  doActivateOfArtificial,
    changeOnOff,
    getOnOffStatus,
} from '@/projects/afs-core-business/api/afs-case/loan-activate/loanActivate'

export default {
  name: 'loan-activate',
  data() {
    return {
      loading: true,
      backLoanModalVisible: false,
      discardModalVisible: false,
      drop: false,
      dropDownContent: '展开',
      dropDownIcon: 'ios-arrow-down',
      selectCount: 0,
      modalTitle: '',
      warningWrods:"",
      isActivate: false,
      activeOnOffVisible:false,
      loanApproveTime:null,
      activeOnOffStatus:"",
      searchFormOptions: [
        {
          defineId: 'loanActivate',
          fields: [
            'applyNo',
            'custName',
            'dealerName',
            'actStatus',
            'loanApproveStatus',
            'gpsApproveStatus',
            'limitApproveStatus',
            'loanApproveTime',
            'businessType',
            'carType',
            'carNature',
            'operateWay',
            'affiliatedWay',
            'lendingMode',
          ],
        },
      ],
      backLoanFormOptions: [
        {
          grids: [
            {
              defineId: 'loanActivate',
              fields: ['backReason'],
            },
          ],
        },
      ],
      discardFormOptions: [
        {
          grids: [
            {
              defineId: 'loanActivate',
              fields: ['discardReason'],
            },
          ],
        },
      ],
      searchForm:
          {
            pageNumber: 1,
            pageSize: 10,
            applyNo: '',
            custName: '',
            dealerName: '',
            actStatus: '',
            loanApproveStatus: '',
            gpsApproveStatus: '',
            limitApproveStatus: '',
            businessType: '',
            carType: '',
            carNature: '',
            operateWay: '',
            affiliatedWay: '',
            lendingMode: ''
        },
      dicKeys: [
            'activateStatus',
            'isType',
            'confBusinessType',
            'conApplyStatus',
            'carType',
            'carNature',
            'operateWay',
            'affiliatedWay',
            'lendingMode'
        ],
      columns: [
          {
              type: 'selection',
              minWidth: 60,
              maxWidth: 60,
              align: 'center',
              fixed: 'left'
          },
          {
              title: "申请编号",
              fixed: "left",
              key: "applyNo",
              width: 165,
              align: "center",
          },
          {
              title: "客户姓名",
              fixed: "left",
              key: "custName",
              width: 165,
              align: "center",
          },
          {
              title: "合同号",
              fixed: "left",
              key: "contractNo",
              width: 165,
              align: "center",
          },
          {
              title: "经销商名称",
              key: "dealerName",
              width: 165,
              align: "center",
          },
          {
              title: "放款审核状态",
              key: "loanApproveStatus",
              width: 165,
              slot: "loanApproveStatus",
          },
          {
              title: "放款审核通过时间",
              key: "loanApproveTime",
              width: 165,
              align: "center",
          },
          {
              title: "GPS审核状态",
              key: "gpsApproveStatus",
              width: 165,
              align: "center",
              slot: "gpsApproveStatus"
          },
          {
              title: "GPS审核通过时间",
              key: "gpsApproveTime",
              width: 165,
              align: "center",
          },
          {
              title: "额度判断状态",
              key: "limitApproveStatus",
              width: 165,
              align: "center",
              slot: "limitApproveStatus"
          },
          {
              title: "额度审核通过时间",
              key: "limitApproveTime",
              width: 165,
              align: "center",
          },
          {
              title: "激活状态",
              key: "actStatus",
              width: 165,
              align: "center",
              slot: "actStatus"
          },
          {
              title: "激活时间",
              key: "actTime",
              width: 165,
              align: "center",
          },
          {
              title: "激活类型",
              key: "actType",
              width: 150,
              align: "center"
          },
          {
              title: "业务类型",
              key: "businessType",
              width: 165,
              align: "center",
              slot: "businessType"
          },
          {
              title: "车辆类型",
              key: "carType",
              width: 165,
              align: "center",
              slot: "carType"
          },
          {
              title: "车辆属性",
              key: "carNature",
              width: 165,
              align: "center",
              slot: "carNature"
          },
          {
              title: "运营方式",
              key: "operateWay",
              width: 165,
              align: "center",
              slot: "operateWay"
          },
          {
              title: "挂靠方式",
              key: "affiliatedWay",
              width: 165,
              align: "center",
              slot: "affiliatedWay"
          },
          {
              title: "放款模式",
              key: "lendingMode",
              width: 165,
              align: "center",
              slot: "lendingMode"
          },
          {
              title: "合同系统落库结果",
              key: "contractActivateFlag",
              width: 165,
              align: "center",
          },
      ],
      data:[],
      total:0,
    }
  },
  computed: {
    defines() {
      return [
        {
          id: 'loanActivate',
          fields: loanActivateDefine,
        },
      ]
    },
  },
  methods: {
      init() {
          this.searchForm.pageNumber = 1
          this.searchForm.pageSize = 10
          this.initDataDic()
          this.getTaskList()
      },
      initDataDic() {
          let self = this
          getByTypes(this.dicKeys).then(res => {
              if (res.code === '0000' && res.data) {
                  self.dataDic = res.data
              }
          })
      },
      rowClassName (row, index) {
          if (row.isLock =="yes") {
              return 'demo-table-lock-row';
          }
          if(row.isOverdue=="yes"){
              return 'demo-table-overdue-row';
          }
          if(row.needReview=="yes"){
              return 'demo-table-retrial-row';
          }
          if(row.isOverTime=='yes'){
              return 'demo-table-overtime-row';
          }
          return '';
      },
      handleSearch() {
          this.searchForm.pageNumber = 1
          this.searchForm.pageSize = 10
          this.getTaskList()
      },
      getTaskList(){
          /**获取业务数据*/
          this.loading=true
          getLoanActivateList(this.searchForm).then((res) => {
              this.loading= false
              if (res.code === '0000') {
                  this.data = res.data.records
                  this.total = res.data.total
              }
          })
          /**获取激活开关状态*/
          getOnOffStatus().then(res=>{
              if (res.code === '0000') {
                  if(res.data.paramValue=="yes"){
                      this.isActivate=true
                  }else {
                      this.isActivate=false
                  }
              }
          });
      },
      setCarType(val){
          let dic = {}
          this.dataDic.carType.forEach(colunm => {
              if (colunm.value === val) {
                  dic = colunm
              }
          })
          return dic.title
      },
      setLendingMode(val){
          let dic = {}
          this.dataDic.lendingMode.forEach(colunm => {
              if (colunm.value === val) {
                  dic = colunm
              }
          })
          return dic.title
      },
      setOperateWay(val){
          let dic = {}
          this.dataDic.operateWay.forEach(colunm => {
              if (colunm.value === val) {
                  dic = colunm
              }
          })
          return dic.title
      },
      setAffiliatedWay(val){
          let dic = {}
          this.dataDic.affiliatedWay.forEach(colunm => {
              if (colunm.value === val) {
                  dic = colunm
              }
          })
          return dic.title
      },
      setCarNature(val){
          let dic = {}
          this.dataDic.carNature.forEach(colunm => {
              if (colunm.value === val) {
                  dic = colunm
              }
          })
          return dic.title
      },
      setBusinessType(val) {
          let dic = {}
          this.dataDic.confBusinessType.forEach(colunm => {
              if (colunm.value === val) {
                  dic = colunm
              }
          })
          return dic.title
      },
      setActStatus(val) {
          let dic = {}
          this.dataDic.activateStatus.forEach(colunm => {
              if (colunm.value === val) {
                  dic = colunm
              }
          })
          return dic.title
      },
      setGpsApproveStatus(val) {
        let dic = {}
        this.dataDic.isType.forEach(colunm => {
            if (colunm.value === val) {
                dic = colunm
            }
        })
        return dic.title
      },
      setApplyStatusTitle(val) {
          let dic = {}
          this.dataDic.conApplyStatus.forEach(colunm => {
              if (colunm.value === val) {
                  dic = colunm
              }
          })
          return dic.title
      },
      dropDown() {
          if (this.drop) {
              this.dropDownContent = '展开'
              this.dropDownIcon = 'ios-arrow-down'
          } else {
              this.dropDownContent = '收起'
              this.dropDownIcon = 'ios-arrow-up'
          }
          this.drop = !this.drop
      },
      selectDateRange(v) {
          if (v) {
              this.searchForm.startDate = v[0]
              this.searchForm.endDate = v[1]
          }
      },
      handleReset() {
          this.$refs.searchForm.resetFields()
          this.loanApproveTime = null
          this.searchForm = {
              pageNumber: 1,
              pageSize: 10,
              applyNo: '',
              custName: '',
              dealerName: '',
              actStatus: '',
              loanApproveStatus: '',
              gpsApproveStatus: '',
              limitApproveStatus: '',
              businessType: '',
              carType: '',
              carNature: '',
              operateWay: '',
              affiliatedWay: '',
              lendingMode: ''
          },
          // 重新加载数据
          this.getTaskList()
      },
      clearSelectAll() {
          this.$refs.table.selectAll(false);
      },
      changeSelect(e) {
          this.selectList = e
          this.selectCount = e.length
      },
      changeSort(e) {
          this.searchForm.sort = e.key
          this.searchForm.order = e.order
          if (e.order === 'normal') {
              this.searchForm.order = ''
          }
          this.getTaskList()
      },
      showSelect(e) {
          this.selectList = e
          this.selectCount = e.length
      },
      changePage(v) {
          this.searchForm.pageNumber = v
          this.getTaskList()
          this.clearSelectAll()
      },
      changePageSize(v) {
          this.searchForm.pageSize = v
          this.getTaskList()
      },
    //填写退回原因
    backLoan() {
      if (this.selectCount <= 0) {
        this.$Message.warning('您还未选择要退回的数据!')
        return
      } else if (this.selectCount > 1) {
        this.$Message.warning('无法执行批量退回操作!')
        return
      }
      let flag = false
      this.selectList.forEach((e) => {
        if (e.actStatus && e.actStatus === 'unActivate') {
          flag = true
        }
      })
      if (!flag) {
        this.$Message.warning('非未激活数据不可退回!')
        return
      }
      this.$refs.loanForm1.resetFrom()
      this.modalType = 0
      this.modalTitle = '操作'
      this.backLoanModalVisible = true
      this.$refs.loanForm1.setAllFieldRequired(true)
    },
    //填写废弃原因
    discard() {
      if (this.selectCount <= 0) {
        this.$Message.warning('您还未选择要废弃的数据!')
        return
      } else if (this.selectCount > 1) {
        this.$Message.warning('无法执行批量废弃操作!')
        return
      }
      let flag = false
      this.selectList.forEach((e) => {
        if (e.actStatus && e.actStatus === 'unActivate') {
          flag = true
        }
      })
      if (!flag) {
        this.$Message.warning('非未激活数据不可执行废弃操作!')
        return
      }
      this.$refs.loanForm2.resetFrom()
      this.modalType = 0
      this.modalTitle = '操作'
      this.discardModalVisible = true
      this.$refs.loanForm2.setAllFieldRequired(true)
    },
    //废弃案件提交
    submitDiscard() {
      this.$Modal.confirm({
        title: '确认废弃',
        content: '您确认要将该条数据废弃么?',
        onOk: () => {
          let ids = []
          this.selectList.forEach(function (e) {
            ids += e.id + ','
          })
          ids = ids.substring(0, ids.length - 1)
          this.$refs.loanForm2.getForm().validate((valid) => {
            if (valid) {
              submitDiscardData(ids).then((res) => {
                if (res.code === '0000') {
                  this.$Message.success(res.msg)
                  this.discardModalVisible = false
                  this.clearSelectAll()
                  this.getTaskList()
                }
              })
            } else {
              this.$Message.error('数据不完整!')
            }
          })
        },
      })
    },
    //退回放款提交
    submitBackLoan() {
      this.$Modal.confirm({
        title: '确认退回',
        content: '您确认要将该条数据退回么?',
        onOk: () => {
          let ids = []
          this.selectList.forEach(function (e) {
            ids += e.id + ','
          })
          ids = ids.substring(0, ids.length - 1)
          this.$refs.loanForm1.getForm().validate((valid) => {
            if (valid) {
              submitBackLoanData(ids).then((res) => {
                if (res.code === '0000') {
                  this.$Message.success(res.msg)
                  this.backLoanModalVisible = false
                  this.clearSelectAll()
                  this.getTaskList()
                }
              })
            } else {
              this.$Message.error('数据不完整!')
            }
          })
        },
      })
    },
    //人工激活
    doActivate() {
      if (this.selectCount <= 0) {
        this.$Message.warning('您还未选择需要激活的数据!')
        return
      }
      let flag ;
      this.selectList.forEach((e) => {
        if (e.actStatus && e.actStatus === 'unActivate') {
          flag = 'unActivate'
        }
      })
      if (flag != 'unActivate') {
        this.$Message.warning('非未激活数据不可激活!')
        return
      }
      this.$Modal.confirm({
        title: '确认激活',
        content: '您确认要执行人工激活操作么?',
        onOk: () => {
          let ids = []
          this.selectList.forEach(function (e) {
            ids += e.id + ','
          })
          ids = ids.substring(0, ids.length - 1)
          doActivateOfArtificial(ids).then((res) => {
            if (res.code === '0000') {
              this.$Message.success(res.msg)
            } else {
              this.$Message.error(res.msg)
            }
          })
          this.clearSelectAll()
          this.getTaskList()
        },
      })
    },
      activateButton(status){
          this.activeOnOffVisible=true;
          this.activeOnOffStatus=status;

          if(status=="yes"){
              this.warningWrods="是否确认开打自动激活开关！";
          }else{
              this.warningWrods="是否确认关闭自动激活开关！";
          }
      },
      confirmButton(status){
          changeOnOff(status).then(res=>{
              if (res.code === '0000') {
                  this.$Message.success(res.msg)
              } else {
                  this.$Message.error(res.msg)
              }
          })
          this.activeOnOffVisible=false;
      },
      cancelButton(){
          this.activeOnOffVisible=false;
      },
      handleBeforeChange () {
          return new Promise((resolve) => {
              this.$Modal.confirm({
                  title: '请确认',
                  content: '您确认要切换自动激活开关状态吗？',
                  onOk: () => {
                      let changeStatus="";
                      if(this.isActivate==false){
                          changeStatus="yes"
                      }else{
                          changeStatus="no"
                      }
                      changeOnOff(changeStatus).then(res=>{
                          if (res.code === '0000') {
                              this.$Message.success(res.msg)
                              resolve();
                          } else {
                              this.$Message.error(res.msg)
                          }
                      })
                  }
              });
          });
      }

  },
  /*mounted() {
    this.init()
  },*/
    activated() {
        this.init()
    },
}
</script>
